//
//  WBPicBroserController.swift
//  WeiboTen
//
//  Created by ItHeiMa on 2017/7/15.
//  Copyright © 2017年 itHeima. All rights reserved.
//

import UIKit

class WBPicBroserController: UIViewController {
    var currentIndex: Int
    var pictureUrls: [WBStatusPicModel]
    
    //重载了默认的init()方法之后，init()就失效了
    //在swift中，没有什么都做，会有一个默认的init方法（）
    init(currentIndex: Int, pictureUrls: [WBStatusPicModel]) {
        self.currentIndex = currentIndex
        self.pictureUrls = pictureUrls
        
        //需要手动调用父类的指定构造器
        super.init(nibName: nil, bundle: nil)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = UIColor.white
        setupUI()
    }
}

extension WBPicBroserController {
    func setupUI() {
        //1. 创建pageViewController
        let pageViewController = UIPageViewController(transitionStyle: .scroll, navigationOrientation: .horizontal, options: [UIPageViewController.OptionsKey.interPageSpacing: 20])
        
        //2. 添加pageviewController
        self.addChild(pageViewController)
        self.view.addSubview(pageViewController.view)
        pageViewController.didMove(toParent: self)
        
        //3. 设置子控制器
        let child = WBPicViewerController(currentIndex: currentIndex, picModel: pictureUrls[currentIndex])
        
        let viewControllers = [child]
        pageViewController.setViewControllers(viewControllers, direction: .forward, animated: true, completion: nil)
        
        //4. 设置并实现数据源
        pageViewController.dataSource = self
        
        //5. 点击事件
        let tap = UITapGestureRecognizer(target: self, action: #selector(back))
        //pageViewController.view.addGestureRecognizer(tap)
        //self.view.gestureRecognizers = pageViewController.gestureRecognizers
        self.view.addGestureRecognizer(tap)
    }
}

extension WBPicBroserController {
    @objc func back() {
        dismiss(animated: false, completion: nil)
    }
}

extension WBPicBroserController: UIPageViewControllerDataSource {
    
    /// 创建前一个控制器
    ///
    /// - Parameters:
    ///   - pageViewController: 当前pageViewController
    ///   - viewController: 当前显示的子控制器
    /// - Returns: 前一个控制器
    func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? {
        //获取当前显示在屏幕上的子控制器的index
        let index = (viewController as! WBPicViewerController).currentIndex
        if index == 0 {return nil}
        
        let childBefore = WBPicViewerController(currentIndex: index - 1, picModel: pictureUrls[index-1])
        return childBefore
    }
    
    /// 创建后一个控制器
    ///
    /// - Parameters:
    ///   - pageViewController: 当前pageViewController
    ///   - viewController: 当前显示的子控制器
    /// - Returns: 后一个控制器
    func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController? {
        //获取当前显示在屏幕上的子控制器的index
        let index = (viewController as! WBPicViewerController).currentIndex
        if index == pictureUrls.count-1 {return nil}
        
        let childBefore = WBPicViewerController(currentIndex: index+1, picModel: pictureUrls[index+1])
        
        return childBefore
    }
}
